<template>
  <div class="inner-container">
    <el-row :gutter="10" style="margin-bottom:8px">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="showAddDialog">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="tableData" row-key="id" :expand-row-keys="expandRowKeys"
              :tree-props="{children: 'children'}">
      <el-table-column prop="name" label="菜单名称" width="180"/>
      <el-table-column prop="type" label="菜单类型">
        <template #default="scope">
          <el-tag v-if="scope.row.type === 'M'">目录</el-tag>
          <el-tag v-else-if="scope.row.type === 'C'" type="success">菜单</el-tag>
          <el-tag v-else-if="scope.row.type === 'F'" type="info">按钮</el-tag>
          <el-tag v-else-if="scope.row.type === 'L'" type="warning">链接</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="icon" label="图标">
        <template #default="scope">
          <el-icon v-if="scope.row.icon && scope.row.icon !== '#'">
            <component :is="scope.row.icon"/>
          </el-icon>
        </template>
      </el-table-column>
      <el-table-column prop="sort" label="排序序号"/>
      <el-table-column prop="component" label="组件路径" show-overflow-tooltip/>
      <el-table-column prop="path" label="路由地址" show-overflow-tooltip/>
      <el-table-column prop="perms" label="权限标识" show-overflow-tooltip/>
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="scope">
          <el-button type="primary" link icon="Edit" @click="showEditDialog(scope.row)">修改</el-button>
          <el-button type="primary" link icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" :before-close="closeDialog" append-to-body>
      <el-form ref="dialogFormRef" :model="formData" :rules="formRules" label-width="80px">
        <el-form-item label="上级菜单" prop="parentId">
          <el-tree-select
              :data="treeSelectData"
              v-model="formData.parentId"
              node-key="id"
              :render-after-expand="false"
              :check-strictly="true"
              :default-expanded-keys="[0]"
              :props="{label: 'name', children: 'children'}"
              clearable
              placeholder="顶级菜单"
              style="width: 100%;"
          />
        </el-form-item>
        <el-form-item label="菜单类型" prop="type">
          <el-radio-group v-model="formData.type">
            <el-radio value="M">目录</el-radio>
            <el-radio value="C">菜单</el-radio>
            <el-radio value="F">按钮</el-radio>
            <el-radio value="L">链接</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入菜单名称"/>
        </el-form-item>
        <el-form-item label="排序序号" prop="sort">
          <el-input v-model.number="formData.sort" type="number" placeholder="请输入排序序号"/>
        </el-form-item>
        <el-form-item label="菜单图标" prop="icon" v-if="formData.type === 'M' || formData.type === 'C'">
          <el-icon-picker v-model="formData.icon"/>
        </el-form-item>
        <el-form-item label="组件路径" prop="component" v-if="formData.type === 'C' || formData.type === 'L'">
          <el-input v-model="formData.component" placeholder="请输入组件路径"/>
        </el-form-item>
        <el-form-item label="路由地址" prop="path" v-if="formData.type === 'C' || formData.type === 'L'">
          <el-input v-model="formData.path" placeholder="请输入路由地址"/>
        </el-form-item>
        <el-form-item label="权限标识" prop="perms" >
          <el-input v-model="formData.perms" placeholder="请输入权限标识"/>
        </el-form-item>
        <el-form-item label="显示状态" prop="visible">
          <el-radio-group v-model="formData.visible">
            <el-radio :value="0">显示</el-radio>
            <el-radio :value="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer class="dialog-footer">
        <el-button type="primary" @click="saveOrUpdate">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {computed, onMounted, ref} from 'vue'
import {ElMessage, ElMessageBox} from 'element-plus'
import ElIconPicker from '@/components/ElIconPicker'
import request from '@/utils/request'
import store from "@/store";

// --------------------------------------------------
const loading = ref(false)
const tableData = ref([])
const expandRowKeys = ref([])

const listData = () => {
  loading.value = true
  request.get('/api/sys/menus')
      .then(res => {
        tableData.value = res.data;
        // 默认展开第一级菜单
        for (let item of tableData.value) {
          expandRowKeys.value.push(item.id.toString())
        }
        loading.value = false
      })
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确定删除吗？', '提示', {
    type: 'warning'
  }).then(() => {
    request.delete(`/api/sys/menus?ids=${row.id}`)
        .then(res => {
          listData()
          ElMessage.success('删除成功')
          getMenuList()
        })
  }).catch(() => {})
}

// --------------------------------------------------
const dialogTitle = ref('')
const dialogVisible = ref(false)
const dialogFormRef = ref()
const formData = ref({
  id: undefined,
  parentId: 0,
  type: 'M',
  name: undefined,
  icon: undefined,
  sort: undefined,
  component: undefined,
  path: undefined,
  perms: undefined,
  visible: 0
})
const formRules = ref({
  name: [{required: true, message: '菜单名称为必填项'}],
  sort: [{required: true, message: '排序序号为必填项'}],
  component: [{required: true, message: '组件路径为必填项'}],
  path: [{required: true, message: '路由地址为必填项'}],
  perms: [{required: true, message: '权限标识为必填项'}]
})

const showAddDialog = () => {
  dialogTitle.value = '新增菜单'
  dialogVisible.value = true
}

const showEditDialog = (row) => {
  let id = row.id
  request.get(`/api/sys/menus/${id}`)
      .then(res => {
        formData.value = res.data
        dialogTitle.value = '修改菜单'
        dialogVisible.value = true
      })
}

const closeDialog = () => {
  formData.value = {
    id: undefined,
    parentId: 0,
    type: 'M',
    name: undefined,
    icon: undefined,
    sort: undefined,
    component: undefined,
    path: undefined,
    perms: undefined,
    visible: 0
  }
  dialogFormRef.value.resetFields()
  dialogVisible.value = false
}

const saveOrUpdate = () => {
  dialogFormRef.value.validate(valid => {
    if (!valid) {
      ElMessage.error('请正确填写参数')
      return
    }
    const id = formData.value.id
    request({
      url: '/api/sys/menus',
      method: id ? 'put' : 'post',
      data: formData.value
    }).then(res => {
      closeDialog()
      listData()
      ElMessage.success(id ? '修改成功' : '新增成功')
      getMenuList()
    });
  })
}

const treeSelectData = computed(() => {
  return [{id: 0, name: '顶级菜单', children: [...tableData.value]}]
})

// --------------------------------------------------
onMounted(() => {
  listData()
})

const getMenuList = () =>{
  request.get('/api/sys/menus').then(res =>{
    if(res.code==200){
      store.commit('setMenus',res.data)

    }
  })
}

</script>

<style scoped>

</style>
